package com.servlet.web;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.servlet.bean.QueryVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

/**
 * @Title: RequestServlet04
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2023/12/21 15:49
 */
@WebServlet("/req04")
public class RequestServlet04 extends HttpServlet {

    private ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 获取请求头信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        // 获取指定的请求头
        String token = req.getHeader("Token");
        System.out.println(token);
        System.out.println("==========================");
        // 获取所有的请求头
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = headerNames.nextElement();
            String value = req.getHeader(key);
            System.out.println(key+"\t"+value);
        }
        resp.getWriter().write("success!");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
